#!/c/Python33/python

#shebang line shall provide the python path
#from tempfile import mkstemp
#from shutil import move
#from os import remove, close
#from os import system
#def replace(file_path, pattern, subst):
#    #Create temp file
#    fh, abs_path = mkstemp()
#    new_file = open(abs_path,'w')
#    old_file = open(file_path)
#    print(old_file)
#    for line in old_file:
#        new_file.write(line.replace(pattern, subst))
#    #close temp file
#    new_file.close()
#    close(fh)
#    old_file.close()
#    #Remove original file
#    remove(file_path)
#    #Move new file
#    move(abs_path, file_path)

import fileinput
import sys
def replaceAll(file,searchExp,replaceExp,count):
    for line in fileinput.input(file, inplace=1):
        if searchExp in line:
            line = line.replace(searchExp,replaceExp,count)
        sys.stdout.write(line)

import os
if os.path.exists("cscope.file"):
  os.remove("cscope.file")

if os.path.exists("tags"):
  os.remove("tags")



#using >ctags --list-maps=all   to check the exact language mapped file extendsion

findcmd = "find . -name '*.py' -o -name '*.pyx' -o -name '*.pxd' -o -name '*.pxi' -o -name '*.scons' > cscope.files"
os.system(findcmd)
#os.system("find . -name '*.c' -o -name '*.h' -o -name '*.cpp' -o -name '*.sig' -o -name '*.cc' -o -name '*.hpp' > cscope.files")
absPath = os.path.abspath(".")
findAction = findcmd + ' under ' + absPath
print(findAction)
replaceAll("cscope.files",".",absPath,1)
print("generate cscope database:: cscope.out cscope.in.out cscope.po.out")
os.system("cscope -Ubkq")
print("generate ctags database for Python:: tags")
os.system("ctags -a -f tags -F --fields=+imnlSfkst --language-force=Python -L cscope.files --links=yes --totals=yes ")

